package com.net.wanjian.networkhospitalmanager.fragment.skilltrain;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.activity.skilltrain.LabDetailsActivity;
import com.net.wanjian.networkhospitalmanager.adapter.TeacherReserveListAdapter;
import com.net.wanjian.networkhospitalmanager.adapter.base.RecyclerBaseAdapter;
import com.net.wanjian.networkhospitalmanager.bean.StudentLabReserveListResult;
import com.net.wanjian.networkhospitalmanager.consts.AppConsts;
import com.net.wanjian.networkhospitalmanager.consts.ErrorCodeReturn;
import com.net.wanjian.networkhospitalmanager.fragment.base.BaseFragment;
import com.net.wanjian.networkhospitalmanager.net.HttpUtil;
import com.net.wanjian.networkhospitalmanager.netstatus.NetUtils;
import com.net.wanjian.networkhospitalmanager.utils.ProgressDialogUtils;
import com.net.wanjian.networkhospitalmanager.utils.SharedXmlUtil;
import com.net.wanjian.networkhospitalmanager.utils.ToastUtil;
import com.net.wanjian.networkhospitalmanager.utils.URLDecoderUtil;
import com.net.wanjian.networkhospitalmanager.view.NoDataEmptyView;
import com.net.wanjian.networkhospitalmanager.widge.RefreshRecyclerView;

import java.util.List;

import butterknife.BindView;
import io.reactivex.observers.DisposableObserver;

/**
 * Description:
 * Created by lidongcai on 2017/5/11.
 * Email:  lidongcaiocc@163.com
 */
public class TeacherLabReserveFragment extends BaseFragment {


    @BindView(R.id.teacher_reserve_recycler_view)
    RefreshRecyclerView myReserveRecyclerView;
    @BindView(R.id.teacher_lab_no_data_layout)
    NoDataEmptyView noDataLayout;

    private TeacherReserveListAdapter teacherReserveListAdapter;
    private List<StudentLabReserveListResult.DataBean.CurrentLabReserveListBean> mCurrentLabReserveList;
    private int currentNum = AppConsts.currentPageNum, currentPageNum, countNum;

    @Override
    protected void initView() {
        noDataLayout.setNoData(R.string.no_teacher_lab_reserve_text);
        noDataLayout.setOnReLoadClickListener(new NoDataEmptyView.OnReLoadClickListener() {
            @Override
            public void onReloadClick(View v) {
                getTeacherLabReserveHttpRequest();
            }
        });
        myReserveRecyclerView.setEmptyView(noDataLayout);


        myReserveRecyclerView.setRefreshMode(3);
        myReserveRecyclerView.addOnRefreshListener(new RefreshRecyclerView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getTeacherLabReserveHttpRequest();
            }

            @Override
            public void onLoadMore() {
                loadMore();
            }
        });

        myReserveRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        myReserveRecyclerView.refresh();
    }

    private void loadMore() {
        currentPageNum++;
        SharedXmlUtil xmlUtil = SharedXmlUtil.getInstance();
        HttpUtil.teacherLabReserveList(xmlUtil.getDeviceId(), xmlUtil.getToken(), xmlUtil.getHospitalId(), xmlUtil.getUserIdentityId(),
                currentPageNum, currentNum, new DisposableObserver<StudentLabReserveListResult>() {
                    @Override
                    public void onNext(StudentLabReserveListResult studentLabReserveListResult) {
                        if (studentLabReserveListResult.getResult().equals(ErrorCodeReturn.success + "")) {
                            SharedXmlUtil.getInstance().setToken(URLDecoderUtil.getDecoder(studentLabReserveListResult.getData().getToken()));
                            List<StudentLabReserveListResult.DataBean.CurrentLabReserveListBean> list = studentLabReserveListResult.getData().getCurrentLabReserveList();
                            mCurrentLabReserveList.addAll(list);
                            teacherReserveListAdapter.setList(mCurrentLabReserveList);

                            if (list.size() < currentNum || mCurrentLabReserveList.size() >= countNum) {
                                myReserveRecyclerView.setNoMore(true);
                            } else {
                                myReserveRecyclerView.loadMoreComplete();
                            }


                        } else {
                            //查询失败
                            myReserveRecyclerView.loadMoreComplete();

                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        myReserveRecyclerView.loadMoreComplete();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    //获取老师预约列表
    public void getTeacherLabReserveHttpRequest() {
        currentPageNum = 0;
        SharedXmlUtil xmlUtil = SharedXmlUtil.getInstance();
        HttpUtil.teacherLabReserveList(xmlUtil.getDeviceId(), xmlUtil.getToken(), xmlUtil.getHospitalId(), xmlUtil.getUserIdentityId(),
                currentPageNum, currentNum, new DisposableObserver<StudentLabReserveListResult>() {
                    @Override
                    public void onNext(StudentLabReserveListResult studentLabReserveListResult) {
                        if (studentLabReserveListResult.getResult().equals(ErrorCodeReturn.success + "")) {
                            SharedXmlUtil.getInstance().setToken(URLDecoderUtil.getDecoder(studentLabReserveListResult.getData().getToken()));
                            mCurrentLabReserveList = studentLabReserveListResult.getData().getCurrentLabReserveList();

                            countNum = Integer.parseInt(studentLabReserveListResult.getData().getTotalCount());
                            initRecyclerList();
                            if (studentLabReserveListResult.getData().getCurrentLabReserveList().size() < currentNum || mCurrentLabReserveList.size() >= countNum) {
                                myReserveRecyclerView.refreshComplete();
                                myReserveRecyclerView.setNoMore(true);
                            } else {
                                myReserveRecyclerView.refreshComplete();
                                myReserveRecyclerView.setNoMore(false);
                            }
                        } else {
                            myReserveRecyclerView.refreshComplete();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        myReserveRecyclerView.refreshComplete();
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    private void initRecyclerList() {

        teacherReserveListAdapter = new TeacherReserveListAdapter(mContext);
        teacherReserveListAdapter.setList(mCurrentLabReserveList);
        myReserveRecyclerView.setAdapter(teacherReserveListAdapter);


        teacherReserveListAdapter.setOnItemClickListener(new RecyclerBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int pos) {
                Bundle bundle = new Bundle();
                bundle.putString(LabDetailsActivity.LAB_RESERVE_ID_KEY, URLDecoderUtil.getDecoder(mCurrentLabReserveList.get(pos).getLabReserveID()));
                bundle.putInt(LabDetailsActivity.LAB_RESERVE_TYPE_KEY, 22);//这里通过区别老师2的标记，在详情页面判断是否显示回执按钮
                bundle.putSerializable(LabDetailsActivity.LAB_RESERVE_DATA_KEY, mCurrentLabReserveList.get(pos));
                openActivity(LabDetailsActivity.class, bundle);
            }
        });
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {
        noDataLayout.setNoData(R.string.no_teacher_lab_reserve_text);
        myReserveRecyclerView.setEmptyView(noDataLayout);
        getTeacherLabReserveHttpRequest();
    }

    @Override
    protected void onNetworkDisConnected() {
        mCurrentLabReserveList = null;

        noDataLayout.setNoNetWork();
        myReserveRecyclerView.setEmptyView(noDataLayout);
        teacherReserveListAdapter.notifyDataSetChanged();
    }

    public static TeacherLabReserveFragment getInstance() {
        TeacherLabReserveFragment fragment = new TeacherLabReserveFragment();
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_teacher_lab_reserve;
    }

    @Override
    protected void initData() {

    }

    /**
     * 更换老师，activity调用
     */
    public void switchTeacherLabReserveHttpRequest() {
        ProgressDialogUtils.showProgressDialog(mContext, null, R.string.loding_switch_teacher);
        SharedXmlUtil xmlUtil = SharedXmlUtil.getInstance();
        HttpUtil.teacherLabReserveList(xmlUtil.getDeviceId(), xmlUtil.getToken(), xmlUtil.getHospitalId(), xmlUtil.getUserIdentityId(),
                0, 10, new DisposableObserver<StudentLabReserveListResult>() {
                    @Override
                    public void onNext(StudentLabReserveListResult studentLabReserveListResult) {
                        if (studentLabReserveListResult.getResult().equals(ErrorCodeReturn.success + "")) {
                            SharedXmlUtil.getInstance().setToken(URLDecoderUtil.getDecoder(studentLabReserveListResult.getData().getToken()));
                            mCurrentLabReserveList = studentLabReserveListResult.getData().getCurrentLabReserveList();

                            teacherReserveListAdapter.setList(mCurrentLabReserveList);

                            ToastUtil.showToast("更换成功");

                        } else {
                            //查询失败
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        ProgressDialogUtils.dismissProgressDialog();
                    }

                    @Override
                    public void onComplete() {
                        ProgressDialogUtils.dismissProgressDialog();
                    }
                });
    }
}
